/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.zmime.ZMimeMessage;
/*     */ import com.zimbra.cs.mailbox.DeliveryOptions;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mime.MimeVisitor;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.File;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.IOException;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.util.SharedByteArrayInputStream;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestParsedMessage
/*     */   extends TestCase
/*     */ {
/*     */   private static final String SENDER_NAME = "user1";
/*     */   private static final String RECIPIENT_NAME = "user1";
/*  43 */   private static final String NAME_PREFIX = TestParsedMessage.class.getSimpleName();
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private File mFile;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  57 */     cleanUp();
/*     */   }
/*     */   
/*     */   public void testParsedMessage() throws Exception
/*     */   {
/*  62 */     ExpectedResults expected = new ExpectedResults();
/*  63 */     String subject = NAME_PREFIX + " testParsedMessage";
/*  64 */     expected.convertedSubject = subject;
/*  65 */     expected.rawContent = TestUtil.getTestMessage(subject, "user1", "user1", null);
/*  66 */     expected.wasMutated = false;
/*     */     
/*     */ 
/*  69 */     ParsedMessage pm = new ParsedMessage(expected.rawContent.getBytes(), false);
/*  70 */     verifyParsedMessage(pm, expected);
/*  71 */     pm = new ParsedMessage(expected.rawContent.getBytes(), true);
/*  72 */     verifyParsedMessage(pm, expected);
/*     */     
/*     */ 
/*  75 */     this.mFile = File.createTempFile("TestParsedMessage", ".msg");
/*  76 */     FileOutputStream out = new FileOutputStream(this.mFile);
/*  77 */     out.write(expected.rawContent.getBytes());
/*     */     
/*  79 */     pm = new ParsedMessage(this.mFile, null, false);
/*  80 */     verifyParsedMessage(pm, expected);
/*  81 */     pm = new ParsedMessage(this.mFile, null, true);
/*  82 */     verifyParsedMessage(pm, expected);
/*     */     
/*     */ 
/*  85 */     MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(expected.rawContent.getBytes()));
/*  86 */     pm = new ParsedMessage(mimeMsg, false);
/*  87 */     verifyParsedMessage(pm, expected);
/*  88 */     pm = new ParsedMessage(mimeMsg, true);
/*  89 */     verifyParsedMessage(pm, expected);
/*     */   }
/*     */   
/*     */   public void testMimeConverter() throws Exception
/*     */   {
/*  94 */     MimeVisitor.registerConverter(TestMimeVisitor.class);
/*     */     
/*  96 */     ExpectedResults expected = new ExpectedResults();
/*  97 */     String subject = NAME_PREFIX + " testMimeConverter oldsubject";
/*  98 */     expected.convertedSubject = (NAME_PREFIX + " testMimeConverter newsubject");
/*  99 */     expected.rawContent = TestUtil.getTestMessage(subject, "user1", "user1", null);
/* 100 */     expected.wasMutated = false;
/*     */     
/*     */ 
/* 103 */     ParsedMessage pm = new ParsedMessage(expected.rawContent.getBytes(), false);
/* 104 */     verifyParsedMessage(pm, expected);
/* 105 */     pm = new ParsedMessage(expected.rawContent.getBytes(), true);
/* 106 */     verifyParsedMessage(pm, expected);
/*     */     
/*     */ 
/* 109 */     this.mFile = File.createTempFile("TestParsedMessage", ".msg");
/* 110 */     FileOutputStream out = new FileOutputStream(this.mFile);
/* 111 */     out.write(expected.rawContent.getBytes());
/* 112 */     out.close();
/*     */     
/* 114 */     pm = new ParsedMessage(this.mFile, null, false);
/* 115 */     verifyParsedMessage(pm, expected);
/* 116 */     pm = new ParsedMessage(this.mFile, null, true);
/* 117 */     verifyParsedMessage(pm, expected);
/*     */     
/*     */ 
/*     */ 
/* 121 */     MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(expected.rawContent.getBytes()));
/* 122 */     pm = new ParsedMessage(mimeMsg, false);
/* 123 */     assertTrue(new String(pm.getRawData()).contains("oldsubject"));
/* 124 */     assertTrue(getContent(pm.getMimeMessage()).contains("newsubject"));
/* 125 */     assertTrue(pm.getSubject().contains("newsubject"));
/* 126 */     pm = new ParsedMessage(mimeMsg, true);
/* 127 */     assertTrue(new String(pm.getRawData()).contains("oldsubject"));
/* 128 */     assertTrue(getContent(pm.getMimeMessage()).contains("newsubject"));
/* 129 */     assertTrue(pm.getSubject().contains("newsubject"));
/*     */   }
/*     */   
/*     */   private void verifyParsedMessage(ParsedMessage pm, ExpectedResults expected)
/*     */     throws Exception
/*     */   {
/* 135 */     for (int i = 1; i < 3; i++)
/*     */     {
/* 137 */       assertEquals(expected.rawContent, new String(pm.getRawData()));
/* 138 */       assertEquals(expected.convertedSubject, pm.getSubject());
/*     */       
/*     */ 
/* 141 */       String sender = TestUtil.getAddress("user1");
/* 142 */       String recipient = TestUtil.getAddress("user1");
/* 143 */       assertTrue(pm.getSender().contains(sender));
/* 144 */       assertEquals(sender, pm.getSenderEmail());
/* 145 */       assertTrue(pm.getRecipients().contains(recipient));
/*     */       
/*     */ 
/* 148 */       String contentFromStream = new String(ByteUtil.getContent(pm.getRawInputStream(), expected.rawContent.length()));
/* 149 */       assertEquals(expected.rawContent, contentFromStream);
/*     */       
/*     */ 
/* 152 */       assertTrue(getContent(pm.getMimeMessage()).contains(expected.convertedSubject));
/*     */       
/*     */ 
/* 155 */       assertEquals(expected.wasMutated, pm.wasMutated());
/*     */       
/* 157 */       pm.analyzeFully();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMimeMutator()
/*     */     throws Exception
/*     */   {
/* 168 */     MimeVisitor.registerMutator(TestMimeVisitor.class);
/*     */     
/* 170 */     String subject = NAME_PREFIX + " testMimeConverter oldsubject";
/* 171 */     String content = TestUtil.getTestMessage(subject, "user1", "user1", null);
/*     */     
/*     */ 
/* 174 */     ParsedMessage pm = new ParsedMessage(content.getBytes(), false);
/*     */     
/* 176 */     pm = new ParsedMessage(content.getBytes(), true);
/* 177 */     String substring = "newsubject";
/* 178 */     verifyMutatedMessage(pm, substring, true);
/*     */     
/*     */ 
/* 181 */     this.mFile = createTempFile(content);
/* 182 */     pm = new ParsedMessage(this.mFile, null, false);
/* 183 */     verifyMutatedMessage(pm, substring, true);
/* 184 */     this.mFile.delete();
/*     */     
/* 186 */     this.mFile = createTempFile(content);
/* 187 */     pm = new ParsedMessage(this.mFile, null, true);
/* 188 */     verifyMutatedMessage(pm, substring, true);
/*     */     
/*     */ 
/* 191 */     MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
/* 192 */     pm = new ParsedMessage(mimeMsg, false);
/* 193 */     verifyMutatedMessage(pm, substring, true);
/*     */     
/*     */ 
/* 196 */     mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
/* 197 */     pm = new ParsedMessage(mimeMsg, true);
/* 198 */     verifyMutatedMessage(pm, substring, true);
/*     */   }
/*     */   
/*     */   private File createTempFile(String content) throws IOException
/*     */   {
/* 203 */     File file = File.createTempFile("TestParsedMessage", ".msg");
/* 204 */     FileOutputStream out = new FileOutputStream(file);
/* 205 */     out.write(content.getBytes());
/* 206 */     out.close();
/* 207 */     return file;
/*     */   }
/*     */   
/*     */   private void verifyMutatedMessage(ParsedMessage pm, String substring, boolean wasMutated) throws Exception
/*     */   {
/* 212 */     assertEquals(wasMutated, pm.wasMutated());
/* 213 */     assertTrue(pm.getSubject().contains(substring));
/* 214 */     assertTrue(new String(pm.getRawData()).contains(substring));
/*     */     
/* 216 */     byte[] data = ByteUtil.getContent(pm.getRawInputStream(), 0);
/* 217 */     assertTrue(new String(data).contains(substring));
/* 218 */     data = pm.getRawData();
/* 219 */     assertTrue(new String(data).contains(substring));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testGetData()
/*     */     throws Exception
/*     */   {
/* 228 */     String msg = TestUtil.getTestMessage(NAME_PREFIX + " testGetData", "user1", "user1", null);
/*     */     
/*     */ 
/* 231 */     ParsedMessage pm = new ParsedMessage(msg.getBytes(), true);
/* 232 */     runContentTests(msg, pm);
/*     */     
/*     */ 
/* 235 */     this.mFile = File.createTempFile("TestParsedMessage", null);
/* 236 */     FileOutputStream out = new FileOutputStream(this.mFile);
/* 237 */     out.write(msg.getBytes());
/* 238 */     pm = new ParsedMessage(this.mFile, null, true);
/* 239 */     runContentTests(msg, pm);
/*     */     
/*     */ 
/* 242 */     MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(msg.getBytes()));
/* 243 */     pm = new ParsedMessage(mimeMsg, true);
/* 244 */     runContentTests(msg, pm);
/*     */   }
/*     */   
/*     */   private void runContentTests(String originalMsg, ParsedMessage pm) throws Exception
/*     */   {
/* 249 */     int size = originalMsg.length();
/*     */     
/*     */ 
/* 252 */     String msg = new String(ByteUtil.getContent(pm.getRawInputStream(), size));
/* 253 */     assertEquals("expected: " + originalMsg + "\ngot: " + msg, originalMsg, msg);
/*     */     
/*     */ 
/* 256 */     msg = new String(pm.getRawData());
/* 257 */     assertEquals("expected: " + originalMsg + "\ngot: " + msg, originalMsg, msg);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testAddMessage()
/*     */     throws Exception
/*     */   {
/* 265 */     String msg = TestUtil.getTestMessage(NAME_PREFIX + " testAddMessage", "user1", "user1", null);
/*     */     
/*     */ 
/* 268 */     ParsedMessage pm = new ParsedMessage(msg.getBytes(), true);
/* 269 */     runAddMessageTest(msg, pm);
/*     */     
/*     */ 
/* 272 */     this.mFile = File.createTempFile("TestParsedMessage", null);
/* 273 */     FileOutputStream out = new FileOutputStream(this.mFile);
/* 274 */     out.write(msg.getBytes());
/* 275 */     pm = new ParsedMessage(this.mFile, null, true);
/* 276 */     runAddMessageTest(msg, pm);
/*     */     
/*     */ 
/* 279 */     MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(msg.getBytes()));
/* 280 */     pm = new ParsedMessage(mimeMsg, true);
/* 281 */     runAddMessageTest(msg, pm);
/*     */   }
/*     */   
/*     */   private void runAddMessageTest(String originalMsg, ParsedMessage pm) throws Exception
/*     */   {
/* 286 */     Mailbox mbox = TestUtil.getMailbox("user1");
/* 287 */     DeliveryOptions dopt = new DeliveryOptions().setFolderId(2);
/* 288 */     Message msg = mbox.addMessage(null, pm, dopt, null);
/* 289 */     assertEquals(originalMsg, new String(ByteUtil.getContent(msg.getContentStream(), 0)));
/*     */   }
/*     */   
/*     */   private String getContent(MimeMessage msg) throws Exception
/*     */   {
/* 294 */     ByteArrayOutputStream buf = new ByteArrayOutputStream();
/* 295 */     msg.writeTo(buf);
/* 296 */     return new String(buf.toByteArray());
/*     */   }
/*     */   
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 302 */     if (this.mFile != null) {
/* 303 */       this.mFile.delete();
/*     */     }
/* 305 */     MimeVisitor.unregisterConverter(TestMimeVisitor.class);
/* 306 */     MimeVisitor.unregisterMutator(TestMimeVisitor.class);
/* 307 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 312 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   private class ExpectedResults
/*     */   {
/*     */     String convertedSubject;
/*     */     String rawContent;
/*     */     boolean wasMutated;
/*     */     
/*     */     ExpectedResults() {}
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestParsedMessage.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */